

package com.hazelcast.internal.crdt;

import com.hazelcast.internal.crdt.pncounter.PNCounterImpl;
import com.hazelcast.internal.crdt.pncounter.PNCounterReplicationOperation;
import com.hazelcast.internal.crdt.pncounter.operations.AddOperation;
import com.hazelcast.internal.crdt.pncounter.operations.CRDTTimestampedLong;
import com.hazelcast.internal.crdt.pncounter.operations.GetOperation;
import com.hazelcast.internal.serialization.DataSerializerHook;
import com.hazelcast.internal.serialization.impl.FactoryIdHelper;
import com.hazelcast.nio.serialization.DataSerializableFactory;

import static com.hazelcast.internal.serialization.impl.FactoryIdHelper.PN_COUNTER_DS_FACTORY;
import static com.hazelcast.internal.serialization.impl.FactoryIdHelper.PN_COUNTER_DS_FACTORY_ID;

/**
 * Serialization constants for all CRDT related objects
 */
public final class CRDTDataSerializerHook implements DataSerializerHook {
    /**
     * The CRDT (de)serializer factory ID
     */
    public static final int F_ID = FactoryIdHelper.getFactoryId(PN_COUNTER_DS_FACTORY, PN_COUNTER_DS_FACTORY_ID);
    /**
     * The (de)serialization constant for a PNCounter replication operation
     */
    public static final int PN_COUNTER_REPLICATION = 1;
    /**
     * The (de)serialization constant for a PNCounter implementation
     */
    public static final int PN_COUNTER = 2;
    /**
     * The constant for the PNCounter {@link AddOperation}
     */
    public static final int PN_COUNTER_ADD_OPERATION = 3;
    /**
     * The constant for the PNCounter {@link GetOperation}
     */
    public static final int PN_COUNTER_GET_OPERATION = 4;
    /**
     * The constant for the PNCounter {@link CRDTTimestampedLong}
     */
    public static final int CRDT_TIMESTAMPED_LONG = 5;

    @Override
    public int getFactoryId() {
        return F_ID;
    }

    @Override
    public DataSerializableFactory createFactory() {
        return typeId -> switch (typeId) {
            case PN_COUNTER_REPLICATION -> new PNCounterReplicationOperation();
            case PN_COUNTER -> new PNCounterImpl();
            case PN_COUNTER_ADD_OPERATION -> new AddOperation();
            case PN_COUNTER_GET_OPERATION -> new GetOperation();
            case CRDT_TIMESTAMPED_LONG -> new CRDTTimestampedLong();
            default -> null;
        };
    }
}
